﻿package com.ghost.utils
{
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;

    public class BrowserCanvas extends Object
    {
        private var _height:String;
        private var _width:String;
        private var _minHeight:String;
        private var _maxWidth:String;
        private var timerSprite:Sprite;
        private var _maxHeight:String;
        private var _minWidth:String;
        private var containerId:String;
        private var stage:Stage;
        public static const HACK_IE_REPARENT:String = "IEReparent";
        public static const HACK_UNIQUE_ID:String = "uniqueId";
        public static const HACK_MARGIN_BOTTOM:String = "marginBottom";
		//高度最好不要超过浏览器高度（在ie下会有上面界面无法看到 ff正常）
        public function BrowserCanvas(stage_:Stage, swfid_:String = null, ary_:Array = null)
        {
            this.stage = stage_;
            timerSprite = new Sprite();
            _width = String(stage_.stageWidth);
            _height = String(stage_.stageHeight);
            if (ary_ == null)
            {
                ary_ = [HACK_MARGIN_BOTTOM, HACK_IE_REPARENT, HACK_UNIQUE_ID];
            }// end if
            this.containerId =  swfid_ != null ? swfid_ : (ExternalInterface.call(JSScripts.GET_FLASH_ID, stage_.loaderInfo.url));
            if (length != 0)
            {
                ExternalInterface.call(JSScripts.INSERT_BROWSER_HACKS, this.containerId, ary_.join(","));
            }// end if
            return;
        }// end function

        public function set maxWidth(setToSize:String) : void
        {
            this._maxWidth = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        private function update(setToSize:Event) : void
        {
            timerSprite.removeEventListener(Event.ENTER_FRAME, update);
            ExternalInterface.call(JSScripts.RESIZE_CONTAINER, containerId, _width, _height, _minWidth, _minHeight, _maxWidth, _maxHeight);
            return;
        }// end function

        public function set width(setToSize:String) : void
        {
            this._width = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        public function set height(setToSize:String) : void
        {
            this._height = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        public function set maxHeight(setToSize:String) : void
        {
            this._maxHeight = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        private function formatSize(height:String) : String
        {
            if (height == null)
            {
                return "";
            }// end if
            return int(height) == 0 ? (height) : (height + "px");
        }// end function

        public function set minWidth(setToSize:String) : void
        {
            this._minWidth = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        public function set minHeight(setToSize:String) : void
        {
            this._minHeight = formatSize(setToSize);
            invalidate();
            return;
        }// end function

        private function invalidate() : void
        {
            timerSprite.addEventListener(Event.ENTER_FRAME, update);
            return;
        }// end function

    }
}
